package de.tabuma.lib.webservice;

import de.tabuma.lib.webservice.wsdl.WsdlFacade;
import de.tabuma.lib.webservice.response.ResponseFacade;
import java.lang.reflect.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.logging.*;

/**
 *
 * @author alefen
 */
public abstract class WebserviceServlet extends HttpServlet {

    private static final Logger log = Logger.getLogger(WebserviceServlet.class.getName());

    private Hashtable<String,Class> methodList = new Hashtable<String,Class>();
    private static final String WSDL_PARAM = "wsdl";

    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    private String namespace;
    public String getNamespace() {
        return namespace;
    }
    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    private String url;
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }


    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if(request.getQueryString()==null) {
                out.println(ResponseFacade.createResponse(request, methodList, namespace));
            }
            else {
                if(request.getQueryString().equals(WSDL_PARAM)) {
                    out.println(WsdlFacade.getWsdl(url, name, namespace, methodList));
                }
                else {
                    out.println(ResponseFacade.createResponse(request, methodList, namespace));
                }
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public abstract void init() throws ServletException;

    public void addClass(String name, Class clazz) {
        this.methodList.put(name,clazz);
    }

}
